<?php
/**
 * @name FormElement
 * @desc An abstract class declaring the default variables and methods of the form elements.
 * 
 * @author Niels Janssen
 * @since 08-07-2008
 * @package Form
 */
abstract class FormElement extends FormNode implements ArrayAccess {
	
	/**
	 * Label van het textvakje.
	 */
	private $label = '';
	
	/**
	 * Constructor of the FormElement class, sets up basic variables.
	 *
	 * @param String $name
	 */
	public function __construct($name, array $properties = array()) {
		parent::__construct ( $name, $properties );
		
		if (empty ( $this->value )) {
			$this->setProperties( array( 'value' => array_key_exists ( $name, $_POST ) ? $_POST [$name] : '' ) );
		}
	}
	
	public function isValid() {
		if ($this->validator != null) {
			try {
				$this->validator->validate ( $this );
			} catch ( ElementValidationException $e ) {
				$this->errorFound = true;
				$this->errorMessage = $e->getMessage ();
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * Returns the element validation.
	 *
	 * @return ElementValidation
	 */
	public function getValidator() {
		if ($this->validator == null)
			$this->validator = new ElementValidation ( $this );
		
		return $this->validator;
	}
	
	public function getName() {
		return $this->name;
	}
	
	public function compareTo(FormNode $fn) {
		return (! ($fn instanceof FormContainer) && ($fn->value == $this->value));
	}
	
	public function offsetGet( $key )
	{
		return isset( $this->properties[ $key ] ) ? $this->properties[ $key ] : null;
	}
	
	public function offsetExists( $key )
	{
		return isset( $this->properties[ $key ] );
	}
	
	public function offsetUnset( $key ){}
	public function offsetSet( $key, $value ) {}

}
